"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;

var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard").default;

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;
exports.defaultGetValueFromEvent = defaultGetValueFromEvent;

var _objectSpread2 = _interopRequireDefault(require("@babel/runtime/helpers/objectSpread2"));

var _toConsumableArray2 = _interopRequireDefault(require("@babel/runtime/helpers/toConsumableArray"));

var _objectWithoutProperties2 = _interopRequireDefault(require("@babel/runtime/helpers/objectWithoutProperties"));

var _jsxRuntime = require("react/jsx-runtime");

var _proUtils = require("@ant-design/pro-utils");

var _antdV = require("antd-v4");

var _toArray = _interopRequireDefault(require("rc-util/lib/Children/toArray"));

var _react = _interopRequireWildcard(require("react"));

var _createField = require("../../BaseForm/createField");

var _helpers = require("../../helpers");

var _excluded = ["children", "value", "valuePropName", "onChange", "fieldProps", "space", "type", "transform", "convertValue"],
    _excluded2 = ["children", "space", "valuePropName"];
var FieldSetType = {
  space: _antdV.Space,
  group: _antdV.Input.Group
};

function defaultGetValueFromEvent(valuePropName) {
  var event = arguments.length <= 1 ? undefined : arguments[1];

  if (event && event.target && valuePropName in event.target) {
    return event.target[valuePropName];
  }

  return event;
}

var FieldSet = function FieldSet(_ref) {
  var children = _ref.children,
      _ref$value = _ref.value,
      value = _ref$value === void 0 ? [] : _ref$value,
      valuePropName = _ref.valuePropName,
      onChange = _ref.onChange,
      fieldProps = _ref.fieldProps,
      space = _ref.space,
      _ref$type = _ref.type,
      type = _ref$type === void 0 ? 'space' : _ref$type,
      transform = _ref.transform,
      convertValue = _ref.convertValue,
      rest = (0, _objectWithoutProperties2.default)(_ref, _excluded);

  /**
   * 使用方法的引用防止闭包
   *
   * @param fileValue
   * @param index
   */
  var fieldSetOnChange = (0, _proUtils.useRefFunction)(function (fileValue, index) {
    var _fieldProps$onChange;

    var newValues = (0, _toConsumableArray2.default)(value);
    newValues[index] = defaultGetValueFromEvent(valuePropName || 'value', fileValue);
    onChange === null || onChange === void 0 ? void 0 : onChange(newValues);
    fieldProps === null || fieldProps === void 0 ? void 0 : (_fieldProps$onChange = fieldProps.onChange) === null || _fieldProps$onChange === void 0 ? void 0 : _fieldProps$onChange.call(fieldProps, newValues);
  });
  var itemIndex = -1;
  var list = (0, _toArray.default)(children).map(function (item) {
    if ( /*#__PURE__*/_react.default.isValidElement(item)) {
      var _item$type, _item$props, _item$props2;

      itemIndex += 1;
      var index = itemIndex;
      var isProFromItem = // @ts-ignore
      (item === null || item === void 0 ? void 0 : (_item$type = item.type) === null || _item$type === void 0 ? void 0 : _item$type.displayName) === 'ProFormComponent' || (item === null || item === void 0 ? void 0 : (_item$props = item.props) === null || _item$props === void 0 ? void 0 : _item$props.readonly);
      var forkProps = isProFromItem ? (0, _objectSpread2.default)((0, _objectSpread2.default)({
        key: index,
        ignoreFormItem: true
      }, item.props || {}), {}, {
        // 如果不是我们自定义的组件 fieldProps 无法识别
        fieldProps: (0, _objectSpread2.default)((0, _objectSpread2.default)({}, item === null || item === void 0 ? void 0 : (_item$props2 = item.props) === null || _item$props2 === void 0 ? void 0 : _item$props2.fieldProps), {}, {
          onChange: function onChange() {
            fieldSetOnChange(arguments.length <= 0 ? undefined : arguments[0], index);
          }
        }),
        value: value === null || value === void 0 ? void 0 : value[index],
        onChange: undefined
      }) : (0, _objectSpread2.default)((0, _objectSpread2.default)({
        key: index
      }, item.props || {}), {}, {
        value: value === null || value === void 0 ? void 0 : value[index],
        onChange: function onChange(itemValue) {
          var _item$props$onChange, _item$props3;

          fieldSetOnChange(itemValue, index);
          (_item$props$onChange = (_item$props3 = item.props).onChange) === null || _item$props$onChange === void 0 ? void 0 : _item$props$onChange.call(_item$props3, itemValue);
        }
      });
      return /*#__PURE__*/_react.default.cloneElement(item, forkProps);
    }

    return item;
  });
  var Components = FieldSetType[type];

  var _useGridHelpers = (0, _helpers.useGridHelpers)(rest),
      RowWrapper = _useGridHelpers.RowWrapper;
  /** Input.Group 需要配置 compact */


  var typeProps = (0, _react.useMemo)(function () {
    return (0, _objectSpread2.default)({}, type === 'group' ? {
      compact: true
    } : {});
  }, [type]);
  var Wrapper = (0, _react.useCallback)(function (_ref2) {
    var dom = _ref2.children;
    return (0, _jsxRuntime.jsx)(Components, (0, _objectSpread2.default)((0, _objectSpread2.default)((0, _objectSpread2.default)({}, typeProps), space), {}, {
      align: "start",
      children: dom
    }));
  }, [Components, space, typeProps]);
  return (0, _jsxRuntime.jsx)(RowWrapper, {
    Wrapper: Wrapper,
    children: list
  });
};

var BaseProFormFieldSet = /*#__PURE__*/_react.default.forwardRef(function (_ref3, ref) {
  var children = _ref3.children,
      space = _ref3.space,
      valuePropName = _ref3.valuePropName,
      rest = (0, _objectWithoutProperties2.default)(_ref3, _excluded2);
  (0, _react.useImperativeHandle)(ref, function () {
    return {};
  });
  return (0, _jsxRuntime.jsx)(FieldSet, (0, _objectSpread2.default)((0, _objectSpread2.default)((0, _objectSpread2.default)({
    space: space,
    valuePropName: valuePropName
  }, rest.fieldProps), {}, {
    // 把 fieldProps 里的重置掉
    onChange: undefined
  }, rest), {}, {
    children: children
  }));
});

var ProFormFieldSet = (0, _createField.createField)(BaseProFormFieldSet);
var _default = ProFormFieldSet;
exports.default = _default;